package com.pudu.mydemo.key;

import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.pudutech.pdrobot.R;

import java.util.List;

public class KeyboardUtil {
	private Activity mActivity;
	@SuppressWarnings("unused")
	private Context mContext;
	@SuppressWarnings("unused")
	private KeyboardView keyboardView;
	private Keyboard keyAlp;// 字母键盘
	private Keyboard keyDig;// 数字键盘
	public boolean isnun = true;// 是否数字键盘
	public boolean isupper = false;// 是否大写

	private EditText ed;

	public KeyboardUtil(Activity mActivity, Context mContext, EditText edit) {
		this.mActivity=mActivity;
		this.mContext = mContext;
		this.ed = edit;
		keyAlp = new Keyboard(mContext, R.xml.qwerty);
		keyDig = new Keyboard(mContext, R.xml.symbols);
		keyboardView = (KeyboardView) mActivity.findViewById(R.id.keyboard_view);
		keyboardView.setKeyboard(keyDig);
		keyboardView.setEnabled(true);
		keyboardView.setPreviewEnabled(false);
		keyboardView.setOnKeyboardActionListener(listener);
	}

	private OnKeyboardActionListener listener = new OnKeyboardActionListener() {
		@Override
		public void swipeUp() {
		}

		@Override
		public void swipeRight() {
		}

		@Override
		public void swipeLeft() {
		}

		@Override
		public void swipeDown() {
		}

		@Override
		public void onText(CharSequence text) {
		}

		@Override
		public void onRelease(int primaryCode) {
		}

		@Override
		public void onPress(int primaryCode) {
		}

		@Override
		public void onKey(int primaryCode, int[] keyCodes) {
			Editable editable = ed.getText();
			int start = ed.getSelectionStart();
			if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
				hideKeyboard();
//				Toast.makeText(mContext, ed.getText().toString(), Toast.LENGTH_LONG).show();
			} else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
				if (!TextUtils.isEmpty(editable)) {
					if (start > 0) {
						editable.delete(start - 1, start);
					}
				}
			} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换
				changeKey();
				keyboardView.setKeyboard(keyAlp);

			} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换
				if (isnun) {
					isnun = false;
					keyboardView.setKeyboard(keyAlp);
				} else {
					isnun = true;
					keyboardView.setKeyboard(keyDig);
				}
			}else {
				editable.insert(start, Character.toString((char) primaryCode));
			}
		}
	};

	/**
	 * 键盘大小写切换
	 */
	private void changeKey() {
		List<Key> keylist = keyAlp.getKeys();
		if (isupper) {// 大写切换小写
			isupper = false;
			for (Key key : keylist) {
				if (key.label != null && isword(key.label.toString())) {
					key.label = key.label.toString().toLowerCase();
					key.codes[0] = key.codes[0] + 32;
				}
			}
		} else {// 小写切换大写
			isupper = true;
			for (Key key : keylist) {
				if (key.label != null && isword(key.label.toString())) {
					key.label = key.label.toString().toUpperCase();
					key.codes[0] = key.codes[0] - 32;
				}
			}
		}
	}

	public void showKeyboard() {
		int visibility = keyboardView.getVisibility();
		if (visibility == View.GONE || visibility == View.INVISIBLE) {
			keyboardView.setVisibility(View.VISIBLE);
		}
	}

	public void hideKeyboard() {
		int visibility = keyboardView.getVisibility();
		if (visibility == View.VISIBLE) {
			keyboardView.setVisibility(View.INVISIBLE);
		}
	}



	private boolean isword(String str) {
		String wordstr = "abcdefghijklmnopqrstuvwxyz";
		if (wordstr.indexOf(str.toLowerCase()) > -1) {
			return true;
		}
		return false;
	}

	public int[] getKeyboardViewLocation() {
		int[] l = {0, 0};
		keyboardView.getLocationInWindow(l);
		int left = l[0],top = l[1];
		int right = left+keyboardView.getWidth();
		int bottom = top + keyboardView.getHeight();
		int location[] = new int[]{left,right,top,bottom};
		return location;
	}

}
